var idx = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navTilList:[
      "待付款",
      "待发货",
      "待收货",
      "待评价",
    ],
    orderList:[],
    navIdx:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '数据加载中...',
    })
    idx = options.myidx ? options.myidx:0;
    this.setData({
      navIdx:parseInt(idx),
      orderList:[]
    })
    this.onChange(idx);
    
  },
   async clickPay(e){
    var {index,id,totalprice,ordernum} = e.currentTarget.dataset

    var res1 = await wx.cloud.callFunction({
      name:"xiangcun_pay",
      data:{
        orderNum:ordernum,
        totalPrice:totalprice
      }
    })
  var payment = res1.result.payment;
  await wx.requestPayment({
    ...payment
    }).then(async res=>{
      await this.updateOrder(ordernum)
      await this.updateProject(index);
    }).catch(err=>{
      console.log(err);
    })
  },

  async updateOrder(ordernum){
    await wx.cloud.callFunction({
      name:"xiangcun_order_update",
      data:{
        orderNum:ordernum
      }
    })
    },

    async updateProject(index){
      var _carArr = this.data.orderList[index].carArr;
      var carArr = _carArr.map(item=>{
        return {goodid : item.goodid,num:item.num}
      })
     await wx.cloud.callFunction({
      name:"xiangcun_product_update",
      data:{
        carArr
      }
    }).then(res=>{
      var _orderList = this.data.orderList;
      _orderList.splice(index,1);
      this.setData({
        orderList:_orderList
      })
    })
    },

  onChange(e){
      
      idx = e.detail ? e.detail.index : e
      this.getOrderData();
  },

  async clickDelOrder(e){
   var res = await wx.showModal({
      title:"是否确认删除"
    })
    if(!res.confirm) return 
    wx.showLoading({
      title: '数据加载中',
      mask:"true"
    })
    var {orderid,orderindex} = e.currentTarget.dataset;
   await wx.cloud.callFunction({
      name:"xiangcun_order_remove",
      data:{
        _id:orderid
      }
    })
    this.getOrderData();
    wx.hideLoading()
  }, 


  getOrderData(){
    wx.cloud.callFunction({
      name:"xiangcun_order_get",
      data:{
        state:parseInt(idx)
      }
    }).then(res=>{
      res.result.data.forEach(item=>{
        var totalPrice = 0;
        item.carArr.forEach(car=>{
          totalPrice+= car.num*car.price
        })
        item.totalPrice=totalPrice
      })
      this.setData({
        orderList:res.result.data
      })
    })
    wx.hideLoading()
  },
     async clickrev(e){
    var {ordernum,index}=e.currentTarget.dataset;
    var res = await wx.showModal({
      title:"是否确认收货"
    })
    if(!res.confirm) return 
    wx.showLoading({
      title: '数据加载中',
      mask:"true"
    })
   var res = await wx.cloud.callFunction({
      name:"xiangcun_order_update",
      data:{
       orderNum: ordernum,
       type:"shouhuo"
      }
    }) 
    this.getOrderData();
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})